<?php

namespace SL\APIBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use SL\APIBundle\Form\UserType;
use SL\APIBundle\Entity\User;
use SL\APIBundle\Entity\UserRepository;

class UserController extends Controller
{
	public function confirmAction($user_id, $token)
	{
		$repository = $this->getDoctrine()->getManager('api')->getRepository('SLAPIBundle:User');
		
		$user = $repository->findOneBy(array('id' => intval($user_id)));
		if ($user === null)
		{
			throw $this->createNotFoundException('Le compte est invalide');
		}
		
		$utoken = $user->getToken();
		if ($utoken === null)
		{
			throw $this->createAccessDeniedException('Le compte est déjà actif');
		}
		
		if ($utoken == $token)
		{
			$user->setToken(null);
			$em->flush();
			return $this->redirect($this->generateUrl('sl_api_user_edit'), array('user_id' => $user_id));
		}
		else
		{
			throw $this->createAccessDeniedException('Le compte n\'a pas pu être activé suite à un token invalide');
		}
	}
	
	public function editAction($user_id)
	{
			
	}
	
	public function indexProjetAction()
	{
		$repository = $this->getDoctrine()->getManager()->getRepository('SLFrontBundle:Projet');
	
		$projets = $repository->findAll();
		return $this->render('SLBackBundle:Projet:index.html.twig', array('projets' => $projets));
	}
	 
	public function saveProjetAction()
	{
		$request = $this->getRequest();
	
		if ($request->getMethod() == 'POST')
		{
			$repository = $this->getDoctrine()->getManager()->getRepository('SLFrontBundle:Projet');
		
			$projets = $repository->findAll();

			foreach ($projets as $projet)
			{
				$value = explode(",", $request->get(strval($projet->getId())));
				$projet->setPosition(intval($value[0]));
				$projet->setLigne(intval($value[1]));
				
			}
			
			$em = $this->getDoctrine()->getManager();
			$em->flush();
			
		}
		return $this->redirect($this->generateUrl('sl_back_projet_index'));
	}
	
	public function addProjetAction()
	{
		$projet = new Projet;
		
		$form = $this->createForm(new ProjetType, $projet);
		
		$request = $this->get('request');
		if ($request->getMethod() == 'POST')
		{
			$form->bind($request);
			
			if ($form->isValid())
			{
				$projet->setUrl(str_replace(array(" ", ".", ",", "'", "\\", "/", "\"", "&", "é", "~", "$", "#", "|",
												  "è", "`", "ç", "à", "@", "¤", "µ", "ù", "¨", "^", "ë", "ä", "ü"), "-", $projet->getName()));
				
				$em = $this->getDoctrine()->getManager();
				$repository = $em->getRepository('SLFrontBundle:Projet');
				
				if ($repository->findOneBy(array('url' => $projet->getUrl())) != null)
					return $this->render('SLBackBundle:Projet:add.html.twig', 
									array('projet' => $projet, 'form' => $form->createView(), 'error' => "Ce projet existe déjà. Changez de nom"));
				
				$projet->setLigne(4);
				$projet->setPosition($this->getLastPositionOfLine(4) + 1);
				
				$projet->setCouverture(new Image());
				$projet->getCouverture()->setUploadDir("couverture");
				$projet->getCouverture()->file = $projet->file;
			
				$em->persist($projet);
				$em->flush();
				
				return $this->redirect($this->generateUrl('sl_back_projet_edit', array('url' => $projet->getUrl())));
			}
		}
				
		return $this->render('SLBackBundle:Projet:add.html.twig', 
									array('projet' => $projet, 'form' => $form->createView(), 'error' => null));
	}
	
	public function deleteProjetAction($url)
	{
		$repository = $this->getDoctrine()->getManager()->getRepository('SLFrontBundle:Projet');
		
		$projet = $repository->findOneBy(array('url' => $url));
		if ($projet === null)
		{
			throw $this->createNotFoundException('Projet '.$url.' inexistant.');
		}
		
		$form = $this->createFormBuilder()->getForm();
		
		$request = $this->get('request');
		if ($request->getMethod() == 'POST')
		{
			$form->bind($request);
			
			$em = $this->getDoctrine()->getManager();
			
			$images = $projet->getImages();	
			foreach ($images as $image)
				$em->remove($image);
			$em->remove($projet);
			$em->flush();
				
			$this->get('session')->getFlashBag()->add('Administration', 'Le Projet '.$projet->getName().' a bien été supprimé');
				
			return $this->redirect($this->generateUrl('sl_back_projet_index'));
			
		}
				
		return $this->render('SLBackBundle:Projet:delete.html.twig', 
									array('projet' => $projet, 'form' => $form->createView()));
	}
	
	public function editProjetAction($url)
	{
		$repository = $this->getDoctrine()->getManager()->getRepository('SLFrontBundle:Projet');
		
		$projet = $repository->findOneBy(array('url' => $url));
		if ($projet === null)
		{
			throw $this->createNotFoundException('Projet '.$url.' inexistant.');
		}
		
		$images = $projet->getImages();
		
		$form = $this->createForm(new ProjetType, $projet);
		
		$request = $this->get('request');
		if ($request->getMethod() == 'POST')
		{
			$form->bind($request);
			
			if ($form->isValid())
			{
				foreach ($images as $image)
				{
					$pos = $request->get(strval($image->getId()));
					$image->setPosition(intval($pos));
				}
				
				if ($projet->file !== null)
				{
					$projet->setCouverture(new Image());
					$projet->getCouverture()->setUploadDir("couverture");
					$projet->getCouverture()->file = $projet->file;
				}
				
				$em = $this->getDoctrine()->getManager();
				$em->persist($projet);	
				$em->flush();
				
				return $this->redirect($this->generateUrl('sl_back_projet_edit', array('url' => $url)));
			}
		}
				
		return $this->render('SLBackBundle:Projet:edit.html.twig', 
									array('projet' => $projet, 'form' => $form->createView(), 'images' => $images));
	}
}